package com.zhaogang.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhaogang.entity.SaleAccount;
import com.zhaogang.entity.SaleSettleinfo;
import com.zhaogang.service.SaleSettleinfoService;

@Controller
@RequestMapping(value="/sale")
public class SaleSettleinfoController {
	
	@Resource
	private SaleSettleinfoService service;
	
	@RequestMapping(value="/infoShow")
	public ModelAndView InfoShow(HttpServletRequest request){
		ModelAndView view=new ModelAndView();
		HttpSession session=request.getSession();
		try{
			SaleAccount saleAccount=(SaleAccount) session.getAttribute("saleToken");
			System.out.println(saleAccount.getId());
			//商户是否已申请
			SaleSettleinfo settle=service.selectByAccountId(saleAccount.getId());		
			if(settle!=null){	
		//		System.out.println(settle.getCardType()+settle.getSaleType());
				view.addObject("settle", settle);
			}
			
		}catch(Exception e){
			
		}
		view.setViewName("goods/settleinfo");
		return view;
	}
	
//	@ResponseBody
//	@RequestMapping(value="/settle", method = RequestMethod.POST)
//	public int insert(SaleSettleinfo settle,HttpServletRequest request, HttpSession httpSession){
//			HttpSession session=request.getSession();
//			SaleAccount saleAccount=(SaleAccount) session.getAttribute("saleToken");
//			settle.setSaleAccountId(saleAccount.getId());
//			settle.setState(0);
//			settle.setRemark("未审核");
//			
////			service.insert(settle);
//			
////			settle.setAddress(address);
////			System.out.println(settle.getAddress());
//			int t=service.insert(settle);
//			return t;		
//		
////		return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/sale/settleinfo";
//		
//	}
	
	@ResponseBody
	@RequestMapping(value="/settle", method = RequestMethod.POST)
	public void insert(SaleSettleinfo settle,HttpServletRequest request, HttpSession httpSession){
			HttpSession session=request.getSession();
			SaleAccount saleAccount=(SaleAccount) session.getAttribute("saleToken");
			String stateString=request.getParameter("state");
			settle.setSaleAccountId(saleAccount.getId());
			settle.setRemark("未审核");
			//提交申请信息
			if(stateString==""){
				settle.setState(0);
				service.insert(settle);
				System.out.println("提交成功");
			}else{
				//修改申请信息
				int id=Integer.parseInt(request.getParameter("id"));
				settle.setId(id);
				settle.setState(0);
				service.updateById(settle);
				System.out.println("修改成功！");
			}

//		return UrlBasedViewResolver.REDIRECT_URL_PREFIX + "/sale/settleinfo";
		
	}
	
}
